package tuiLayer;
import controlLayer.*;
import modelLayer.*;
import java.util.*;

public class SellUI 
{
    private SellCtr sellCtr;
    private CustomerCtr customerCtr;
    private ItemCtr itemCtr;

    public SellUI(){
        sellCtr = new SellCtr();
        customerCtr = new CustomerCtr();
        itemCtr= new ItemCtr();
    }

    public void start(){
        mainMenuSell();
    }

    private void mainMenuSell(){
        boolean stop=false;
        while(!stop){
            int choice = writeMainMenuSell();
            switch(choice){
                case 0:
                    stop = true;
                    break;             
                case 1:
                    createSell();
                    break;
                case 2:
                    findSell();
                    break;
                case 3:
                    deleteSell();
                    break;
                default : System.out.println("Enter a valid choice");
            }
        }
    }

    private int writeMainMenuSell(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Sell menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Create sell");
        System.out.println("(2) Find sell");
        System.out.println("(3) Delete sell");
        System.out.println("\nType your choice");
        int choice= keyboard.nextInt();
        return choice;
    }

    private void createSell(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Sell creation---");
        System.out.println();

        System.out.println("Enter the date:");
        String date = keyboard.nextLine();

        System.out.println("Enter the customer id:");
        int idC = keyboard.nextInt();
        keyboard.nextLine();

        sellCtr.createSell(date, customerCtr.findCustomer(idC) );

        addItemToSellMenu();

        System.out.println("Sell successfully created!");
    }

    private void addItemToSell(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the barcode of the item:");
        int barcode = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the quantity:");
        int quantity = keyboard.nextInt();
        keyboard.nextLine();

        OrderLineSell orderLineSell= new OrderLineSell(quantity,itemCtr.findItem(barcode));
        sellCtr.addItemToSell(orderLineSell);
        orderLineSell.decreaseNoInStock();
    }

    private void addItemToSellMenu(){
        Scanner keyboard = new Scanner(System.in);
        boolean stop = false;
        while(!stop){
            int choice = writeAddItemToSellMenu();
            switch(choice){
                case 0:
                    stop = true;
                    break;             
                case 1:
                    addItemToSell();
                    break;
                default : System.out.println("Enter a valid choice");
            }
        }
    }

    private int writeAddItemToSellMenu(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Add item to sell menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Add item and quantity");
        int choice= keyboard.nextInt();
        return choice;
    }

    private void findSell(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Sell search---");
        System.out.println();
        System.out.println("Enter the id of the sell:");
        int id = keyboard.nextInt();
        Sell sell= sellCtr.findSell(id);
        System.out.println("--------------------------------");
        System.out.println("Date:\t\t\t "+sell.getDate());
        System.out.println("Customer name:\t\t"+sell.getCustomerName());
        System.out.println("Customer ID:\t\t"+sell.getCustomerId()+"\n");
        System.out.println("BoughtItems:");
        System.out.println(sell.getBoughtItemInfo());
        System.out.println("Price WITHOUT discount:\t"+sell.getTotalPrice());
        System.out.println("Price WITH discount:\t"+sell.discountPrice());
        System.out.println("--------------------------------");
    }

    private void deleteSell(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Sell removal---");
        System.out.println();

        System.out.println("Enter the sell id:");
        int idS = keyboard.nextInt();

        sellCtr.deleteSell(idS);
        System.out.println("Sell successfuly deleted!");
    }
}